#include "Expand/Plugins/ItemPlugin.h"
#include "Expand/PluginsManager.h"

#include "Game/IInventory.hpp"
#include "Managers/BlocksManager.h"
#include "Components/ComponentInventory.h"
#include "Network/Client.h"
#include "Player/PlayerData.h"

#include "Log.h"
#include "GameSingleton.h"
#include "GameRegistry.hpp"

namespace PocketSurvival
{
    const std::string &ItemPlugin::getPluginName()
    {
        static const std::string pluginName = "管理员快捷获取物品";
        return pluginName;
    }

    const std::string &ItemPlugin::getIntroduce()
    {
        static const std::string introduce = "\n/item (物品id) (数量) - 获取物品";
        return introduce;
    }

    uint32_t ItemPlugin::getUseAuthLevel()
    {
        return 100;
    }

    AuthDisplayType ItemPlugin::getDisplayType()
    {
        return AuthDisplayType::Authority;
    }

    void ItemPlugin::processCmd(ChatObj &chatObj, std::vector<std::string> &cmdDataVec)
    {
        if(chatObj.m_isTerminal == true)
        {
            chatObj.sendMessage("item", "控制台不能使用");
            return;
        }
        if(cmdDataVec.size() >= 3)
        {
            int32_t cellValue = atoi(cmdDataVec[1].c_str());
            if (GameSingleton::blocksManager->blocks[Terrain::ExtractContents(cellValue)] == nullptr)
            {
                chatObj.sendMessage("item", "服务器不存在该物品");
                return;
            }
            uint32_t count = atoi(cmdDataVec[2].c_str());
            IInventory *inventory = GameSingleton::gameRegistry->get<ComponentInventory*>(chatObj.m_client->playerData->entity);
            for(uint32_t index = 0; index < inventory->slotsCount(); ++index)
            {
                int32_t slotValue = inventory->getSlotValue(index);
                uint32_t slotCount = inventory->getSlotCount(index);
                if(slotCount != 0 && slotValue != cellValue)
                {
                    continue;
                }
                inventory->setSlotValue(index, cellValue, count + slotCount);
                chatObj.sendMessage("item", "物品已经送达，请注意你的背包");
                return;
            }
            chatObj.sendMessage("item", "请确保背包有空位或有相同的物品");
            return;
        }
        chatObj.sendMessage("item", getIntroduce());
    }

    void ItemPlugin::getCmds(std::vector<std::string> &cmdVec)
    {
        cmdVec.push_back("/item");
    }
} // namespace PocketSurvival
